package com.zrh.jsd.service.studydemo.threadpool;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

public class CompleteFutureExTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        getUserInfo();
    }
    public static Integer getUserInfo() throws InterruptedException, ExecutionException {
        ExecutorService executor = Executors.newCachedThreadPool();

        CompletableFuture userFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("userFuture");
            return Boolean.TRUE;
        }, executor);

        CompletableFuture bonusFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("bonusFuture");
            return b();
        }, executor);

        CompletableFuture growthFuture = CompletableFuture.supplyAsync(() -> {
            a();
            return null;
        }, executor);


//        System.out.println(CompletableFuture.allOf(userFuture, bonusFuture, growthFuture).join());

        System.out.println(userFuture.get());
        Map<String,String> map = (Map<String, String>) bonusFuture.get();
        System.out.println(map.get("a"));
        System.out.println(growthFuture.get());

        return 123;
    }
    public static void a(){
        try {
            System.out.println("===a===start==");
            TimeUnit.SECONDS.sleep(10);
            System.out.println("===a===end==");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static Map<String,String> b(){
        try {
            System.out.println("===b===start==");
            TimeUnit.SECONDS.sleep(10);
            System.out.println("===b===end==");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Map map = new HashMap<String,String>();
        map.put("a","b");
        return map;
    }
}
